package rndgen;

import java.awt.BasicStroke;
import java.awt.Color;
import java.util.ArrayList;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RefineryUtilities;

public class Chart {
	
	/** A frame for displaying the chart. */
    private ChartFrame frame = null;

    /**
     * Displays a sample chart in its own frame.
     */
    void displayChart(ArrayList<Double> sequence, double interval, Double min, Double max) {

        if (this.frame == null) {

            final String title = "Histogram of sequence distribution";
            final String xAxisLabel = "Value's intervals";
            final String yAxisLabel = "Count of elements";

            final XYDataset data = createStepXYDataset(sequence, interval, min, max);

            final JFreeChart chart = ChartFactory.createXYLineChart(
                title,
                xAxisLabel, yAxisLabel,
                data,
                PlotOrientation.VERTICAL,
                true,   // legend
                true,   // tooltips
                false   // urls
            );
           
           
            
            chart.setBackgroundPaint(new Color(216, 216, 216));
            final XYPlot plot = chart.getXYPlot();
           
            plot.getRenderer().setSeriesStroke(0, new BasicStroke(2.0f));
            plot.getRenderer().setSeriesStroke(1, new BasicStroke(2.0f));
       
            this.frame = new ChartFrame("Random sequence", chart);
            this.frame.pack();
            RefineryUtilities.centerFrameOnScreen(this.frame);
            this.frame.setVisible(true);
         
            
           

        }
        else {
            this.frame.setVisible(true);
            this.frame.requestFocus();
        }

    }

    /**
     * Creates and returns a sample dataset for the XY 'Step' chart.
     *
     * @return the sample dataset.
     */
    public static XYDataset createStepXYDataset(ArrayList<Double> sequence, double interval, Double min, Double max) {

       	Integer count = 0;
    	
    	ArrayList<Integer> histogram = new ArrayList<Integer>();
    	
    	for (double i = min; i <=max; i+=interval){
    		for (Double it : sequence){
    			if (it.doubleValue() >= i && it.doubleValue() < i+interval) count++;
    		}
    		histogram.add(count);
    		count = 0;
    	}
    	
    	final XYSeries s = new XYSeries("Exponential distribution", false, true);
        
        for (double i = 0, j = min; i < histogram.size(); i++, j+=interval){
        	s.add(j, histogram.get((int)i));
        	s.add(j+interval, histogram.get((int)i));
        	
        }
        

        final XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(s);
        return dataset;
    }
}
